import React, { useEffect } from 'react';
import { useRequest } from 'ahooks';
import { Request } from '@peace/utils';
import { Request as MockRequest } from './webapi';

export const useFsRequest = ({ ...props }) => {
  const {
    method = 'get', header = null, body = {}, query = {}, root = null, url, ...rest
  } = props;
  return useRequest(() => {
    if (method === 'post') {
      return Request.post(url, body, query, root, header);
    }
    if (method === 'put') {
      return Request.put(url, body, query, root, header);
    }
    if (method === 'delete') {
      return Request.delete(url, query, root, header);
    }
    return Request.get(url, query, root, header);
  }, {
    loadingDelay: 500,
    ...rest,
  });
};

// 第三方系统 获取数据hook , 第三方系统api用这个，包括apiFox 的mock数据 传入不同method即可
export const useMockRequest = ({ ...props }) => {
  const {
    method = 'get', header, body, query, url, ...rest
  } = props;
  return useRequest(() => {
    if (method === 'post') {
      return MockRequest.post(url, body || {}, query || {});
    }
    if (method === 'put') {
      return MockRequest.put(url, body || {}, query || {});
    }
    if (method === 'mockGet') {
      return MockRequest.mockGet(url, query || {}, header || {});
    }
    if (method === 'mockPost') {
      return MockRequest.mockPost(url, body || {}, query || {}, header || {});
    }

    return MockRequest.get(url, query || {});
  }, {
    loadingDelay: 500,
    ...rest,
  });
};


export default {
  useFsRequest,
  useMockRequest
};
